package com.block.datapush.form.item.util;

import com.alibaba.druid.support.json.JSONUtils;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.*;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

public class HuiZhongUtil {

    public static String code = "UTF-8";
    public static String key = "mgjr@3des123#dhjr9763089";

    public static String encrypt(String data, String secretKey) {
        try {
            byte[] encrpyted = tripleDES(Cipher.ENCRYPT_MODE, data.getBytes(code), secretKey.getBytes());
            byte[] encoded = Base64.encodeBase64(encrpyted); // Base64编码
            return new String(encoded);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String decrypt(String data) {
        try {
            byte[] decoded = Base64.decodeBase64(data); // Base64解码
            byte[] decrypted = tripleDES(Cipher.DECRYPT_MODE, decoded, key.getBytes());// 3DES解密
            return new String(decrypted, code);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    private static byte[] tripleDES(int opmode, byte[] data, byte[] secretKey) {
        return cipher("DESede", "DESede/CBC/PKCS5Padding", opmode, data, "01234567".getBytes(), secretKey);
    }

    private static byte[] cipher(String algorithm, String transformation, int opmode, byte[] data, byte[] iv, byte[] secretKey) {
        try {
            Key key = SecretKeyFactory.getInstance(algorithm).generateSecret(new DESedeKeySpec(secretKey));
            IvParameterSpec spec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(opmode, key, spec);
            return cipher.doFinal(data);
        } catch (InvalidKeyException | InvalidKeySpecException | NoSuchAlgorithmException | NoSuchPaddingException
                | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("cust_name", "测试");
            param.put("mobile", "18888888881");
            param.put("age", "30");
            param.put("sex", "1");
            param.put("city", "上海");
            param.put("apply_limit", "50000");
            param.put("channel_source", "chlxx");

            String paramStr = JSONUtils.toJSONString(param);
            String enctryt_data = encrypt(paramStr, key);
            System.out.println("加密:" + enctryt_data);

            String data = decrypt(enctryt_data);
            System.out.println("解密:" + data);

            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("data", enctryt_data);
            String url = "http://apitest.shlyxxjs.com/api/yhzd/addCust";
            /*JSONObject result = postJson(url, data, new HashMap<>());*/



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
